﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.IO;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using FmcWebsite.Web;
using FmcWebsite.Core;
using OboutInc.Show;

namespace Cms
{
    public partial class Article : System.Web.UI.Page
    {
        private const string root_dir = @"D:\Projects\FmcWebsite\Cms\";

        private WebArticle article;

        protected void Page_Load(object sender, EventArgs e)
        {                        

            if (Request.Params["code"] != null)
            {
                article = WebArticle.ByCode(Request.Params["code"], this.Session);
            }

            if ((article == null) && (Request.Params["id"] != null))
            {
                int id;
                if (int.TryParse(Request.Params["id"], out id))
                {
                    article = WebArticle.ById(id, Session);
                }
            }

            if (article != null)
            {
                // get article images
                if (article.Images.Count() > 0)
                {
                    imageShow.Visible = true;
                    if (article.Images.Count() == 1)
                    {
                        imageShow.ShowType = ShowType.Show;
                        imageShow.Height = "400px";
                        imageShow.ManualChanger = false;
                    }
                    else
                    {
                        imageShow.ShowType = ShowType.Show;
                        imageShow.ManualChanger = true;
                    }
                    foreach (AttachmentIMAGE img in article.Images)
                    {
                        imageShow.AddImagePanel("/AttachmentImage.ashx?id=" + img.Id.ToString());
                    }                    
                }
                else
                    imageShow.Visible = false;



            }
        }

        protected void GetTitle()
        {
            if (article != null)
            {
                Response.Write(article.Title);
            }
            else
                Response.Write("Error: article not found");
        }

        protected void GetContent()
        {
            if (article != null)
            {
                Response.Write(article.Content);
            }
        }

        protected void GetAudio()
        {
            AttachmentAUDIO audio = article != null ? article.Audio : null;

            if (audio != null)
            {

                var aud = WebAttachmentAudio.ById(audio.Id, Session);

                Response.Write(
                    string.Format(
                    "<br><object type=\"application/x-shockwave-flash\" data=\"\\etc\\player_mp3_maxi.swf\" width=\"300\" height=\"40\">" +
                    "<param name=\"movie\" value=\"player_mp3_maxi.swf\" />" +
                    "<param name=\"FlashVars\" value=\"mp3={0}&autoplay=1&showvolume=1&showloading=1&volume=150\" />" +
                    "</object><br>", aud.GetUrl()
                    )
                    );
            }
        }

        protected void GetImages()
        {
            if ((article !=null) && (article.Images.Count() > 0))
            {
                StreamReader template = new StreamReader(root_dir + "gallery.htm");
                try
                {
                    var s = template.ReadToEnd();
                    Response.Write(s.Replace("#art_id#", article.Id.ToString()));
                }
                finally
                {
                    template.Close();
                }
            }

        }
    }
}
